#$Id$

package Feeds::GCF::Exception::ExcMEODTrch;
@ISA = qw(Feeds::GCF::Exception::ExcCCBroker);

use strict qw(vars);
use warnings ( FATAL => 'all' );

use Data::Dumper;
use Readonly;
use POSIX qw(strftime);

use Feeds::GCF::Constants qw(:GENERIC :STD_TABLE :STD_TYPE :SYBASE_SQL);
use Feeds::GCF::Util qw(hostname now today redo_sql);
use Feeds::Utils::CDSBrokerConfig;
use Feeds::Utils::CDSBrokerMapper;
use Feeds::GCF::Exception::ExcCCBroker;

sub check_tranche_tier_cd{
 
    my $self   = shift;
    my $asset  = shift;
    my $n_data = shift;
    
   
    my $attach = $n_data->{n_tbl}->{attach}*100;
    my $detach = $n_data->{n_tbl}->{detach}*100;
    my $sql=<<"SQL";
         
    select count(*) as trch_leg_nm
     from n_markiteod_trch
     where idx_nm='$n_data->{n_tbl}->{idx_nm}'
       and attach<=$n_data->{n_tbl}->{attach}
               
SQL

     my $res=$self->{dbh}->selectall_hashes($sql);
     $asset->{mapping}->{contract}->{idx_trch_leg_nm}= $res->[0]->{trch_leg_nm};
     if ($res->[0]->{trch_leg_nm}==0 
         ||$res->[0]->{trch_leg_nm}>6){
        $self->{log}->warning("wrong trch_leg_nm:[$asset->{mapping}->{contract}->{idx_trch_leg_nm}]");
         push @{ $asset->{exc_cd} }, 'WTC';
         return;
     }
       
     $asset->{mapping}->{contract}->{attach}= $attach;
     $asset->{mapping}->{contract}->{detach}= $detach;
      
     ($asset->{mapping}->{contract}->{ric_tranche_cd}
      ,$asset->{mapping}->{contract}->{idx_trch_tier_cd}) = $self->_get_trch_info($asset->{mapping}->{contract}->{idx_trch_leg_nm});
        
   
    
    
    
    
  
    
   
    
    
}

sub _get_trch_info{
    
    my $self = shift;
    my $trch_leg_nm = shift;
    
    my @tier_rics=sort keys %{$self->{tranche_tier_cd_def}};
    
    return ($tier_rics[$trch_leg_nm-1],$self->{tranche_tier_cd_def}->{$tier_rics[$trch_leg_nm-1]});
}



sub check_WCU {
    my $self   = shift;
    my $asset  = shift;
    my $n_data = shift;
    
    my $sql=<<"SQL";
   
      SELECT currency_cd 
        FROM dmo_creditds..cds_index
       WHERE idx_id_nm='$n_data->{n_tbl}->{red_cd}'
SQL

    my $res=$self->{dbh}->selectall_hashes($sql);
    
    if (@$res >=1){
        $n_data->{n_tbl}->{ccy_cd}=$res->[0]->{currency_cd};
    }else{
        $self->{log}->warning("can't find currency_cd for red_cd:$n_data->{n_tbl}->{red_cd}");
        push @{ $asset->{exc_cd} }, 'WCU';
        return;
    }

    my $ret
        = $self->{mapper}->map_ccy( $n_data->{n_tbl}, $self->{xml_config} );
    if (   $ret
        && @$ret == 2 )
    {
        $asset->{mapping}->{contract}->{ric_ccy}     = $ret->[0];
        $asset->{mapping}->{contract}->{currency_cd} = $ret->[1];
    }
    else {
        push @{ $asset->{exc_cd} }, 'WCU';
    }

}

1;
